/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar.wicket;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.swing.border.Border;

import net.stuff.servoy.beans.calendar.DateChooserHelpers;
import net.stuff.servoy.beans.calendar.IDateChooserBeanComponent;

import org.apache.wicket.Page;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.util.convert.IConverter;
import org.wicketstuff.calendar.markup.html.form.DatePicker;
import org.wicketstuff.calendar.util.DateConverter;

import com.servoy.j2db.Messages;
import com.servoy.j2db.dataprocessing.IRecord;
import com.servoy.j2db.dataui.IServoyAwareBean;
import com.servoy.j2db.plugins.IClientPluginAccess;
import com.servoy.j2db.scripting.JSEvent;
import com.servoy.j2db.server.headlessclient.IWebClientPluginAccess;
import com.servoy.j2db.server.headlessclient.MainPage;
import com.servoy.j2db.server.headlessclient.dataui.StyleAttributeModifierModel;
import com.servoy.j2db.server.headlessclient.dataui.TemplateGenerator;
import com.servoy.j2db.server.headlessclient.dataui.TooltipAttributeModifier;
import com.servoy.j2db.ui.ILabel;
import com.servoy.j2db.ui.IStylePropertyChanges;
import com.servoy.j2db.ui.ISupportWebBounds;
import com.servoy.j2db.util.ComponentFactoryHelper;
import com.servoy.j2db.util.Debug;
import com.servoy.j2db.util.PersistHelper;

/**
 * WicketDateChooser is a custom implementation of the Servoy Wicket Date component
 * used to allow easy use of the DateChooserBean and make it fully web compatible
 * 
 * @version 1.2 - 2010-03-01 - Servoy 5+

 * @author Servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class WicketDateChooser extends Panel
implements IDateChooserBeanComponent, IServoyAwareBean,
IStylePropertyChanges, ISupportWebBounds {

	private static final long serialVersionUID = 1L;
	
	public ExtendedChangesRecorder jsChangeRecorder = new ExtendedChangesRecorder(TemplateGenerator.DEFAULT_FIELD_BORDER_SIZE, TemplateGenerator.DEFAULT_FIELD_PADDING);

	private IWebClientPluginAccess app;
	private IRecord currentRecord;
	private boolean validationMode = true;
	private String dataProviderID;
	private String format;
	
	private Dimension size;
	private Border border;
	private Point location;
	private Color background;
	private Color foreground;
	private Font font;
	private String toolTipText;
	private String titleText;
	private String name;
	private boolean opaque;
	private boolean editable;
	private Cursor cursor;
	private int horizontalAlignment;
	private boolean valid = true;
	private boolean selectOnEnter;
	
	private WicketDateTextField field;
	private DatePicker picker;
	private Date date;
	private Date previousDate;
	private Map<Object, Object> clientProperties;
	private List<ILabel> labels;
	
	private Color validColor;
	private Color errorColor;
	
	private String onAction;
	private String onDataChange;
	private String onFocusGained;
	private String onFocusLost;
	
	private boolean valueChanged;
		
	public WicketDateChooser(String id, String format) {
		super(id);
		
		setOutputMarkupPlaceholderTag(true);
		
		add(StyleAttributeModifierModel.INSTANCE);
		
		add(TooltipAttributeModifier.INSTANCE);
		
		setVersioned(false);
		
		jsChangeRecorder.setOverflow("hidden");
		add(new SimpleAttributeModifier("class", getId()+" field"));
		
		this.format = format;
		DateConverter converter = new MutablePatternDateConverter(Messages.getStringIfPrefix(format), true, this);
		field = new WicketDateTextField("dateChooser", new DateStringPropertyModel(this, "date"), converter);
		field.setMarkupId(getId()+field.getId());
		add(field);
		
		picker = new WicketDatePicker(this);
		field.add(picker);
		
		field.add(new DateStringValidator(this));
		field.add(new WicketDateTextFieldEventBehavior(WicketDateTextFieldEventBehavior.KEY_DOWN, this));
		field.add(new WicketDateTextFieldEventBehavior(WicketDateTextFieldEventBehavior.DATA_CHANGE, this));
		field.add(new WicketDateTextFieldEventBehavior(WicketDateTextFieldEventBehavior.FOCUS_GAINED, this));
		field.add(new WicketDateTextFieldEventBehavior(WicketDateTextFieldEventBehavior.FOCUS_LOST, this));
	}
		
	/* (non-Javadoc)
	 * @see org.apache.wicket.Component#getMarkupId()
	 */
	@Override
	public String getMarkupId() {
		return getId();
	}
	
	/* (non-Javadoc)
	 * @see org.apache.wicket.MarkupContainer#onRender(org.apache.wicket.markup.MarkupStream)
	 */
	@Override
	protected void onRender(MarkupStream markupStream) {
		super.onRender(markupStream);
		field.setRendered();
		setRendered();
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#getValidationEnabled()
	 */
	public boolean getValidationEnabled() {
		return validationMode;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getSize()
	 */
	public Dimension getSize() {
		return size;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setSize(java.awt.Dimension)
	 */
	public void setSize(Dimension size) {
		this.size = size;
		if (this.size != null) {
			jsChangeRecorder.setSize(size.width, size.height, this.border, null, 0);
		}
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getBorder()
	 */
	public Border getBorder() {
		return border;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setBorder(javax.swing.border.Border)
	 */
	public void setBorder(Border border) {
		this.border = border;
		if (this.border != null) {
			ComponentFactoryHelper.createBorderCSSProperties(ComponentFactoryHelper.createBorderString(this.border), jsChangeRecorder.getChanges());
		}
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getLocation()
	 */
	public Point getLocation() {
		return location;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setLocation(java.awt.Point)
	 */
	public void setLocation(Point location) {
		this.location = location;
		if (this.location != null) {
			jsChangeRecorder.setLocation(location.x, location.y);
		}
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getBackground()
	 */
	public Color getBackground() {
		return background;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setBackground(java.awt.Color)
	 */
	public void setBackground(Color c) {
		this.background = c;
		if (c != null) {
			String colorString = PersistHelper.createColorString(c);
			jsChangeRecorder.setBgcolor(colorString);
		}
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getForeground()
	 */
	public Color getForeground() {
		return foreground;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setForeground(java.awt.Color)
	 */
	public void setForeground(Color c) {
		this.foreground = c;
		if (c != null) {
			field.setFgcolor(c);
		}
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getFont()
	 */
	public Font getFont() {
		return font;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setFont(java.awt.Font)
	 */
	public void setFont(Font font) {
		this.font = font;
		field.setFont(font);
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getToolTipText()
	 */
	public String getToolTipText() {
		return toolTipText;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setToolTipText(java.lang.String)
	 */
	public void setToolTipText(String tooltip) {
		this.toolTipText = Messages.getStringIfPrefix(tooltip);
	}
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#getName()
	 */
	public String getName() {
		return name;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setName(java.lang.String)
	 */
	public void setName(String name) {
		this.name = name;
		if (name != null) {
			add(new SimpleAttributeModifier("name", name));
		}
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setComponentEnabled(boolean)
	 */
	public void setComponentEnabled(boolean enabled) {
		setEnabled(enabled);
		field.setEnabled(enabled);
		jsChangeRecorder.setChanged();
	}

	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setCursor(java.awt.Cursor)
	 */
	public void setCursor(Cursor cursor) {
		this.cursor = cursor;
	}
	public Cursor getCursor() {
		return cursor;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#isOpaque()
	 */
	public boolean isOpaque() {
		return opaque;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setOpaque(boolean)
	 */
	public void setOpaque(boolean b) {
		opaque = b;
		jsChangeRecorder.setTransparent(!b);
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IComponent#setComponentVisible(boolean)
	 */
	public void setComponentVisible(boolean visible) {
		setVisible(visible);
		jsChangeRecorder.setVisible(visible);
		field.setVisible(visible);
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.ISupportWebBounds#getWebBounds()
	 */
	public Rectangle getWebBounds() {
	    Dimension localDimension = this.jsChangeRecorder.calculateWebSize(this.size.width, this.size.height, this.border, null, 0, null);
	    return new Rectangle(this.location, localDimension);
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#getChanges()
	 */
	public Properties getChanges() {
		return jsChangeRecorder.getChanges();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#isChanged()
	 */
	public boolean isChanged() {
		return jsChangeRecorder.isChanged();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setChanged()
	 */
	public void setChanged() {
		jsChangeRecorder.setChanged();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setChanges(java.util.Properties)
	 */
	public void setChanges(Properties paramProperties) {
		jsChangeRecorder.setChanges(paramProperties);
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setRendered()
	 */
	public void setRendered() {
		jsChangeRecorder.setRendered();
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.dataprocessing.IDisplay#isReadOnly()
	 */
	public boolean isReadOnly() {
		return field.isReadOnly();
	}

	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.dataprocessing.IDisplay#setValidationEnabled(boolean)
	 */
	public void setValidationEnabled(boolean validationMode) {
		this.validationMode = validationMode;
	}
	

	/* (non-Javadoc)
	 * @see com.servoy.j2db.dataprocessing.IDisplay#stopUIEditing(boolean)
	 */
	public boolean stopUIEditing(boolean looseFocus) {
		return (validationMode) ? valid : true;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.dataui.IServoyAwareBean#initialize(com.servoy.j2db.plugins.IClientPluginAccess)
	 */
	public void initialize(IClientPluginAccess app) {
		this.app = (IWebClientPluginAccess)app;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.dataui.IServoyAwareBean#setSelectedRecord(com.servoy.j2db.dataprocessing.IRecord)
	 */
	public void setSelectedRecord(IRecord record) {
		currentRecord = record;
		if (!DateChooserHelpers.isEmpty(dataProviderID)) {
			date = (Date)currentRecord.getValue(dataProviderID);
			previousDate = (date != null) ? (Date)date.clone() : null;
			Debug.log("setSelectedRecord "+date);
			if (validationMode) {
				field.setModelObject(date);
			}
			resetForeground();
			jsChangeRecorder.setChanged();
			valueChanged = false;
		}
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#getFormat()
	 */
	public String getFormat() {
		return this.format;
	}

	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setFormat(java.lang.String)
	 */
	public void setFormat(String dateFormatString) {
		this.format = Messages.getStringIfPrefix(dateFormatString);
		IConverter converter = field.getConverter(MutablePatternDateConverter.class);
		if (converter != null && converter instanceof MutablePatternDateConverter) {
			//formatter = new SimpleDateFormat(format);
			((MutablePatternDateConverter)converter).setDatePattern(format);
			setChanged();
		}
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return this.date;
	}

	/**
	 * @param date the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
		if (!DateChooserHelpers.isEmpty(dataProviderID)) {
			if (currentRecord.startEditing()) {
				if (!DateChooserHelpers.useTime(format)) {
					date = DateChooserHelpers.resetDate(date);
				}
				currentRecord.setValue(dataProviderID, date);
			}
		}
	}
	
	/**
	 * @param date 
	 * set the date as String
	 */
	public void setDate(String date) {
		if (!DateChooserHelpers.isEmpty(dataProviderID)) {
			if (currentRecord.startEditing()) {
				currentRecord.setValue(dataProviderID, date);
			}
		}
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#getHorizontalAlignment()
	 */
	public int getHorizontalAlignment() {
		return horizontalAlignment;
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setHorizontalAlignment(int)
	 */
	public void setHorizontalAlignment(int paramInt) {
		this.horizontalAlignment = paramInt;
		field.setHorizontalAlignment(paramInt);
	}



	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#getDataProviderID()
	 */
	public String getDataProviderID() {
		return dataProviderID;
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setDataProviderID(java.lang.String)
	 */
	public void setDataProviderID(String paramString) {
		this.dataProviderID = paramString;
		
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setErrorColor(java.awt.Color)
	 */
	public void setErrorColor(Color errorColor) {
		this.errorColor = errorColor;
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setValidColor(java.awt.Color)
	 */
	public void setValidColor(Color validColor) {
		this.validColor = validColor;
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setOnAction(java.lang.String)
	 */
	public void setOnAction(String onAction) {
		this.onAction = onAction;
		
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setOnDataChange(java.lang.String)
	 */
	public void setOnDataChange(String onDataChange) {
		this.onDataChange = onDataChange;
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setOnFocusGained(java.lang.String)
	 */
	public void setOnFocusGained(String onFocusGained) {
		this.onFocusGained = onFocusGained;
	}


	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserComponent#setOnFocusLost(java.lang.String)
	 */
	public void setOnFocusLost(String onFocusLost) {
		this.onFocusLost = onFocusLost;
	}


	/**
	 * @return the validColor
	 */
	public Color getValidColor() {
		return this.validColor;
	}


	/**
	 * @return the errorColor
	 */
	public Color getErrorColor() {
		return this.errorColor;
	}


	/**
	 * @return the onAction
	 */
	public String getOnAction() {
		return this.onAction;
	}


	/**
	 * @return the onDataChange
	 */
	public String getOnDataChange() {
		return this.onDataChange;
	}


	/**
	 * @return the onFocusGained
	 */
	public String getOnFocusGained() {
		return this.onFocusGained;
	}


	/**
	 * @return the onFocusLost
	 */
	public String getOnFocusLost() {
		return this.onFocusLost;
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#setEditable(boolean)
	 */
	public void setEditable(boolean editable) {
		setComponentEnabled(editable);
	}
	
	public boolean isEditable() {
		return isEnabled(); 
	}
	
	private Map getClientProperties() {
		if (clientProperties == null) {
			clientProperties = new HashMap<Object, Object>();
		}
		return clientProperties;
	}
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_isVisible()
	 */
	public boolean js_isVisible() {
		return isVisible();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setVisible(boolean)
	 */
	public void js_setVisible(boolean visible) {
		setComponentVisible(visible);
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getBgcolor()
	 */
	public String js_getBgcolor() {
		return (getBackground() == null) ? null : PersistHelper.createColorString(getBackground());
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getClientProperty(java.lang.Object)
	 */
	public Object js_getClientProperty(Object paramObject) {
		return getClientProperties().get(paramObject);
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getElementType()
	 */
	public String js_getElementType() {
		return ELEMENT_TYPE;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getFgcolor()
	 */
	public String js_getFgcolor() {
		return (getForeground() == null) ? null : PersistHelper.createColorString(getForeground());
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getHeight()
	 */
	public int js_getHeight() {
		return getSize().height;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getLocationX()
	 */
	public int js_getLocationX() {
		return getLocation().x;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getLocationY()
	 */
	public int js_getLocationY() {
		return getLocation().y;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getName()
	 */
	public String js_getName() {
		return getName();
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getWidth()
	 */
	public int js_getWidth() {
		return getSize().width;
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_isEnabled()
	 */
	public boolean js_isEnabled() {
		return isEnabled();
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_putClientProperty(java.lang.Object, java.lang.Object)
	 */
	public void js_putClientProperty(Object paramObject1, Object paramObject2) {
		getClientProperties().put(paramObject1, paramObject2);
	}

	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setBgcolor(java.lang.String)
	 */
	public void js_setBgcolor(String paramString) {
		setBackground(PersistHelper.createColor(paramString));
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setBorder(java.lang.String)
	 */
	public void js_setBorder(String paramString) {
		setBorder(ComponentFactoryHelper.createBorder(paramString));
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setEnabled(boolean)
	 */
	public void js_setEnabled(boolean paramBoolean) {
		setComponentEnabled(paramBoolean);
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setFgcolor(java.lang.String)
	 */
	public void js_setFgcolor(String paramString) {
		setForeground(PersistHelper.createColor(paramString));
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setLocation(int, int)
	 */
	public void js_setLocation(int paramInt1, int paramInt2) {
		setLocation(new Point(paramInt1, paramInt2));
	}


	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_setSize(int, int)
	 */
	public void js_setSize(int paramInt1, int paramInt2) {
		setSize(new Dimension(paramInt1, paramInt2));
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptFormattedFieldMethods#js_getFormat()
	 */
	public String js_getFormat() {
		return getFormat();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptFormattedFieldMethods#js_setFormat(java.lang.String)
	 */
	public void js_setFormat(String dateFormatString) {
		setFormat(dateFormatString);
	}
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptReadOnlyMethods#js_isReadOnly()
	 */
	public boolean js_isReadOnly() {
		return isReadOnly();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptReadOnlyMethods#js_setReadOnly(boolean)
	 */
	public void js_setReadOnly(boolean readOnly) {
		field.setReadOnly(readOnly);
		jsChangeRecorder.setChanged();
	}
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTransparentMethods#js_setToolTipText(java.lang.String)
	 */
	public void js_setToolTipText(String paramString) {
		setToolTipText(paramString);
		jsChangeRecorder.setChanged();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTransparentMethods#js_getToolTipText()
	 */
	public String js_getToolTipText() {
		return getToolTipText();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTransparentMethods#js_setFont(java.lang.String)
	 */
	public void js_setFont(String paramString) {
		if (paramString != null) {
			setFont(PersistHelper.createFont(paramString));
		}
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTransparentMethods#js_isTransparent()
	 */
	public boolean js_isTransparent() {
		return !isOpaque();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTransparentMethods#js_setTransparent(boolean)
	 */
	public void js_setTransparent(boolean transparent) {
		setOpaque(!transparent);
	}
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptInputMethods#js_isEditable()
	 */
	public boolean js_isEditable() {
		return editable;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptInputMethods#js_setEditable(boolean)
	 */
	public void js_setEditable(boolean paramBoolean) {
		this.editable = paramBoolean;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptInputMethods#js_requestFocus(java.lang.Object[])
	 */
	public void js_requestFocus(Object[] paramArrayOfObject) {
		Page page = getPage();
		if (page instanceof MainPage) {
			((MainPage)page).componentToFocus(field);
		}
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptInputMethods#js_getDataProviderID()
	 */
	public String js_getDataProviderID() {
		return getDataProviderID();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptInputMethods#js_getLabelForElementNames()
	 */
	public String[] js_getLabelForElementNames() {
		if (labels != null) {
			List temp = new ArrayList<String>(labels.size());
			for (Iterator iterator = labels.iterator(); iterator.hasNext();) {
				ILabel label = (ILabel) iterator.next();
				if (label.getName() != null && !"".equals(label.getName().trim())) {
					temp.add(label.getName());
				}
			}
			return (String[])temp.toArray(new String[temp.size()]);
		}
		return new String[0];
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#fireActionCallBack(java.lang.Object[])
	 */
	public void fireActionCallBack() {
		String method = getOnAction();
		if (DateChooserHelpers.isNotEmpty(method)) {
			DateChooserHelpers.runFunctionByName(method, app, new Object[] {createJSEvent(method)});
		}
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#fireDataChangeCallBack(java.lang.Object[])
	 */
	public void fireDataChangeCallBack(Object[] params) {
		String method = getOnDataChange();
		if (DateChooserHelpers.isNotEmpty(method) && previousDate != getDate()) {
			DateChooserHelpers.runFunctionByName(method, app, new Object[] {createJSEvent(method), previousDate, date});
			previousDate = (Date)date.clone();
		}
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#fireFocusLostCallBack(java.lang.Object[])
	 */
	public void fireFocusLostCallBack() {
		String method = getOnFocusLost();
		if (DateChooserHelpers.isNotEmpty(method)) {
			DateChooserHelpers.runFunctionByName(method, app, new Object[] {createJSEvent(method)});
		}
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#fireFocusGainedCallBack(java.lang.Object[])
	 */
	public void fireFocusGainedCallBack() {
		String method = getOnFocusGained();
		if (DateChooserHelpers.isNotEmpty(method)) {
			DateChooserHelpers.runFunctionByName(method, app, new Object[] {createJSEvent(method)});
		}
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#setValid()
	 */
	public void setValid() {
		valid = true;
		field.setFgcolor(validColor);
		setChanged();
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#setInvalid()
	 */
	public void setInvalid() {
		valid = false;
		field.setFgcolor(errorColor);
		jsChangeRecorder.setChanged();
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#resetForeground()
	 */
	public void resetForeground() {
		valid = true;
		field.setFgcolor(foreground);
		jsChangeRecorder.setChanged();
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#getDateTextField()
	 */
	public Object getDateTextField() {
		return field;
	}
	
	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#isSelectOnEnter()
	 */
	public boolean isSelectOnEnter() {
		return this.selectOnEnter;
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#setSelectOnEnter(boolean)
	 */
	public void setSelectOnEnter(boolean selectOnEnter) {
		this.selectOnEnter = selectOnEnter;
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#getTitleText()
	 */
	public String getTitleText() {
		return titleText;
	}

	/* (non-Javadoc)
	 * @see net.stuff.servoy.beans.calendar.IDateChooserBeanComponent#setTitleText(java.lang.String)
	 */
	public void setTitleText(String titleText) {
		this.titleText = Messages.getStringIfPrefix(titleText);
	}

	public boolean testChanged() {
		if (previousDate != getDate()) {
			setChanged();
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#isValueChanged()
	 */
	public boolean isValueChanged() {
		try {
			return jsChangeRecorder.isValueChanged();
		} catch (Exception ex) {}
		return valueChanged;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IStylePropertyChanges#setValueChanged()
	 */
	public void setValueChanged() {
		try {
			jsChangeRecorder.setValueChanged();
		} catch (Exception ex) {}
		this.valueChanged = true;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptBaseMethods#js_getAbsoluteFormLocationY()
	 */
	public int js_getAbsoluteFormLocationY() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.IScriptTitleTextMethods#js_getTitleText()
	 */
	public String js_getTitleText() {
		return titleText;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.ui.ISupportWebBounds#getPaddingAndBorder()
	 */
	public Insets getPaddingAndBorder() {
		return TemplateGenerator.DEFAULT_FIELD_PADDING;
	}
		
	protected JSEvent createJSEvent(final String method) {
		final JSEvent event = new JSEvent();
		event.setElementName(getName());
		if (app != null) {
			String frmName = null;
			if (DateChooserHelpers.isGlobal(method)) {
				frmName = "undefined";
			} else {
				final int indexOfDot = method.indexOf('.');
				if (indexOfDot > -1) {
					frmName = method.substring(0, indexOfDot);
				}
			}
			if (frmName == null) {
				event.setFormName(app.getFormManager().getCurrentForm().getName());
			} else {
				event.setFormName(frmName);
			}
		}
		event.setSource(this);
		event.setType(JSEvent.NONE);
		event.setTimestamp(new Date());
		return event;
	}

}
